# -*- coding: utf-8 -*-
"""
@Time ： 2023/4/2 15:17
@Auth ： daiminggao
@File ：main.py
@IDE ：PyCharm
@Motto:咕咕咕
"""
import csv
import math

import pandas as pd
import numpy as np
from numpy import array
from scipy.io import arff
from time import time
from Stream import Stream
from Point_object import point_object
import os
import psutil


def start():
    global new_point
    dataset = "kddcup(reduce)"
    datas = pd.read_csv("./dataset/zhao/" + dataset + ".csv", header=None)
    data_list = np.array(datas)
    stream = Stream(data_list, dataset, 0.001)
    count = math.floor(len(data_list) / 1000)
    # count = 9
    shifting = 0
    # print("data_list:", data_list)

    for i in range(count):
        start_time = time()  # 算法开始时间
        satrt_memory = show_info()
        for j in range(1000):
            new_point = point_object(data_list[shifting + j][-1],
                                     data_list[shifting + j][0:-2])  # 取出每项数据的时间戳和2个维度的数值 zjj
            stream.fuzzy_query(stream.min_distance, stream.max_distance, new_point, stream.point_list)
            for k in new_point.kernel:
                k.kernel = set(k.kernel) | {new_point, }
            for s in new_point.shell:
                s.shell = set(s.shell) | {new_point, }
            stream.point_list.append(new_point)
        shifting = shifting + 1000
        if new_point.point_time_stamp % stream.time_offline == 0:
            print("=============================t=" + str(new_point.point_time_stamp) + "=============================")
            stream.offline_FDBSCAN(stream.point_list, new_point.point_time_stamp, start_time, satrt_memory)
            stream.num_offline = stream.num_offline + 1


def show_info():
    # 计算消耗内存
    pid = os.getpid()
    # 模块名比较容易理解：获得当前进程的pid
    p = psutil.Process(pid)
    # 根据pid找到进程，进而找到占用的内存值
    info = p.memory_full_info()
    memory = info.uss / 1024 / 1024
    return memory
    # print(f'{start} 一共占用{memory:.2f}MB')


if __name__ == '__main__':
    start()
